<!-- 编辑弹窗 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate?'修改用户':'添加用户'"
    :visible="visible"
    custom-class="ele-dialog-form"
    width="800px"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-row :gutter="20">
        <el-col :sm="12">
          <el-form-item label="用户账户:" prop="username">
            <el-input
              v-model="form.username"
              :disabled="isUpdate"
              :maxlength="20"
              clearable
              placeholder="请输入账户"/>
          </el-form-item>

          <el-form-item label="角色:" prop="role">
            <el-select
              v-model="form.role"
              class="ele-block"
              clearable
              placeholder="请选择角色">
              <el-option
                v-for="item in roleList"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"/>
            </el-select>
          </el-form-item>

          <el-form-item label="联系电话:">
            <el-input
              v-model="form.phone"
              :maxlength="20"
              clearable
              placeholder="请输入联系电话"/>
          </el-form-item>

          <el-form-item label="邮箱:">
            <el-input
              v-model="form.email"
              :maxlength="100"
              clearable
              placeholder="请输入邮箱"/>
          </el-form-item>

          <el-form-item
            label="状态:">
            <el-switch
              v-model="form.status"
              :active-value="1"
              :inactive-value="0"/>
            <el-tooltip
              content="选择关闭将无法进行登录"
              placement="top">
              <i class="el-icon-_question" style="vertical-align: middle;margin-left: 8px;"></i>
            </el-tooltip>
          </el-form-item>

          <el-form-item label="备注:">
            <el-input
              v-model="form.remark"
              :maxlength="200"
              :rows="4"
              clearable
              placeholder="请输入备注信息"
              type="textarea"/>
          </el-form-item>

        </el-col>

        <el-col :sm="12">
          <el-form-item
            label="登录密码:" prop="password">
            <el-input
              v-model="form.password"
              :maxlength="20"
              placeholder="请输入登录密码"
              show-password/>
          </el-form-item>

          <el-form-item label="负责人:" prop="nickname">
            <el-input
              v-model="form.nickname"
              :maxlength="20"
              clearable
              placeholder="请输入负责人"/>
          </el-form-item>

          <el-form-item label="联系微信:">
            <el-input
              v-model="form.wechat"
              :maxlength="20"
              clearable
              placeholder="请输入联系电话"/>
          </el-form-item>

        </el-col>


        <el-col :sm="12">
          <el-form-item label="允许登录IP:">
            <el-input
              v-model="form.loginip"
              :maxlength="20"
              clearable
              placeholder="8.8.8.*,9.9.9.9."/>
          </el-form-item>
          <el-form-item label="允许登录时段:">
            <el-input
              v-model="form.logintime"
              clearable
              placeholder="10-12, 20-22"/>
          </el-form-item>
        </el-col>


        <el-col :sm="12">

          <el-form-item label="允许登录城市:">
            <el-input
              v-model="form.logincity"
              clearable
              placeholder="北京,上海,天津"/>
          </el-form-item>

        </el-col>

      </el-row>
    </el-form>
    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save">确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({
        status: 1
      }, this.data),
      // 表单验证规则
      rules: {
        username: [
          {
            required: true,
            type: 'string',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('请输入用户账户'));
              }
              this.$http.get('/user/query?username=' + value).then(res => {
                if (res.data.code !== 0 || !res.data.data.length) {
                  return callback();
                }
                if (this.isUpdate && res.data.data[0].username === this.data.username) {
                  return callback();
                }
                callback(new Error('账户已经存在'));
              }).catch(() => {
                callback();
              });
            }
          }
        ],
        password: [
          {
            required: true,
            type: 'string',
            trigger: 'blur',
            validator: (rule, value, callback) => {
              if (!this.isUpdate && !value) {
                return callback(new Error('请输入密码'));
              }
              callback();
            }
          }
        ],
        role: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 角色列表
      roleList: []
    };
  },
  watch: {
    data() {
      if (Object.keys(this.data).length > 0) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.form.status = 1;
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    this.queryRoles();  // 搜索角色列表
  },
  methods: {
    /* 确定编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/user/' + [this.isUpdate ? 'update' : 'create'], this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                for (let i in this.form) {
                  this.form[i] = null;
                }
                this.form.status = 1;
              }
              this.updateVisible(false);
              this.$emit('done', this.isUpdate);
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /* 搜索角色列表 */
    queryRoles() {
      this.$http.get('/role/role').then(res => {
        if (res.data.code === 0) {
          this.$emit('childFn', res.data.data);
          this.roleList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
